package petertel.pbx.conf.sip;
import java.util.Iterator;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import petertel.pbx.conf.ConfigItem;
import petertel.pbx.conf.NameValuePair;
import petertel.pbx.connector.Executor;

@Controller
@Scope(value = "session")
@RequestMapping(value = "/sipConf")
public class SipConf extends ConfigItem<Device> {
	
	private String deviceName;

	private Device currentDevice;

	private boolean toggle;
	public boolean getToggle() {
		return toggle = !toggle;
	}

	@RequestMapping(value = "/show")
	public String show(String deviceName) {
		this.setDeviceName(deviceName);
		return doShow();
	}

	private String doShow() {
		return "sipConf_show";
	}

	@RequestMapping(value = "/insert")
	public String insert(String deviceName, String propertyName) {
		this.setDeviceName(deviceName);
		NameValuePair prop = new NameValuePair();
		prop.setName(propertyName);
		prop.setFlag(Flag.insert);
		currentDevice.addChild(-1, prop);
		return doShow();
	}
	
	@RequestMapping(value="/save")
	@ResponseBody
	public boolean save(String deviceName,int index,String propertyName,String propertyValue){
		this.setDeviceName(deviceName);
		NameValuePair property = currentDevice.getChildren().get(index);
		if (property.getFlag() != Flag.insert) {
			property.setFlag(Flag.update);
		}
		property.setName(propertyName);
		property.setValue(propertyValue);
		return true;
	}

	@RequestMapping(value = "/reset")
	public String reset(String deviceName) {
		SipConfBuilder.build(new Executor(), this);
		this.setDeviceName(deviceName);
		return doShow();
	}

	@RequestMapping(value = "/apply")
	public String apply(String deviceName) {
		this.setDeviceName(deviceName);
		if (this.currentDevice != null) {
			for (Iterator<NameValuePair> iter = currentDevice.getChildren()
					.iterator(); iter.hasNext();) {
				NameValuePair prop = iter.next();
				if (prop.getName() == null) {
					iter.remove();
				}
			}
		}
		SipConfWriter.write(new Executor(), this);
		return reset(deviceName);
	}

	@RequestMapping(value = "/remove")
	public String remove(String deviceName, int index) {
		this.setDeviceName(deviceName);
		NameValuePair prop = currentDevice.getChildren().get(index);
		if (prop.getFlag() == Flag.insert) {
			currentDevice.removeChildren(index);
		} else {
			currentDevice.getChildren().get(index).setFlag(Flag.delete);
		}
		return doShow();
	}
	
	@RequestMapping(value="/addDevice")
	public String addDevice(String deviceName){
		Device device = new Device();
		device.setFlag(Flag.insert);
		device.setName(deviceName);
		this.addChild(-1, device);
		this.setDeviceName(deviceName);
		SipConfWriter.write(new Executor(), this);
		return reset(deviceName);
	}
	
	@RequestMapping(value="/removeDevice")
	public String removeDevice(String deviceName){
		List<Device> devices = getChildren();
		for(int i=0;i<devices.size();i++){
			Device device = devices.get(i);
			if(device.getName().equals(deviceName)){
				removeChildren(i);
			}
		}
		setDeviceName(null);
		return doShow();
	}

	public void setDevices(List<Device> devices) {
		this.children = devices;
	}

	@Override
	public String write() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String toString() {
		String ret = "";
		for (int i = 0; i < children.size(); i++) {
			ret += children.get(i);
		}
		return ret;
	}

	public void setDeviceName(String deviceName) {
		this.deviceName = deviceName;
		this.currentDevice = null;
		for (Device device : getChildren()) {
			if (device.getName().equals(deviceName)) {
				this.currentDevice = device;
				break;
			}
		}
	}


	public Device getCurrentDevice() {
		return currentDevice;
	}

	public String getApplyButtonClass() {
		if (isDirty())
			return "button-red";
		return "button-blue";
	}

	public boolean isDirty() {
		if (currentDevice == null) {
			return false;
		}
		return currentDevice.isDirty();
	}

	public String getDeviceName() {
		return deviceName;
	}

}
